{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Exercise -Version Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- This is the exercise based on the forked repository on GitHub\n",
    "-  Help to improve it if you want, just add the solution under the question\n",
    "- Each notebook only contains 10 questions which is convenient for everyone to locate each question to modify, you can add your own name under the author's list below, let's contribute together !\n",
    "- To visit my personal site: [Click Here](http://yaozeliang.com/resume)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 21\n",
    "\n",
    "\n",
    ">***A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:***\n",
    "```\n",
    "UP 5\n",
    "DOWN 3\n",
    "LEFT 3\n",
    "RIGHT 2\n",
    "```\n",
    ">***The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.***\n",
    "***Example:***\n",
    "***If the following tuples are given as input to the program:***\n",
    "```\n",
    "UP 5\n",
    "DOWN 3\n",
    "LEFT 3\n",
    "RIGHT 2\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "2\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "pos=[0,0]\n",
    "while(True):\n",
    "    value=input().split(\" \")\n",
    "    if value==['']:\n",
    "        break\n",
    "    operation1=value[0]\n",
    "    operation2=int(value[1])\n",
    "    if operation1==\"UP\":\n",
    "        pos[1]=pos[1]+operation2\n",
    "    elif operation1==\"DOWN\":\n",
    "        pos[1]=pos[1]-operation2\n",
    "    elif operation1==\"LEFT\":\n",
    "        pos[0]=pos[0]-operation2\n",
    "    elif operation1==\"RIGHT\":\n",
    "        pos[0]=pos[0]+operation2\n",
    "    else:\n",
    "        pass\n",
    "\n",
    "print(int(np.math.sqrt(pos[0]**2+pos[1]**2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 22\n",
    "\n",
    ">***Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.***\n",
    "\n",
    ">***Suppose the following input is supplied to the program:***\n",
    "```\n",
    "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.\n",
    "```\n",
    ">***Then, the output should be:***\n",
    "```\n",
    "2:2\n",
    "3.:1\n",
    "3?:1\n",
    "New:1\n",
    "Python:5\n",
    "Read:1\n",
    "and:1\n",
    "between:1\n",
    "choosing:1\n",
    "or:2\n",
    "to:1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "New : 1\n",
      "to : 1\n",
      "Python : 5\n",
      "or : 2\n",
      "choosing : 1\n",
      "between : 1\n",
      "2 : 2\n",
      "and : 1\n",
      "3? : 1\n",
      "Read : 1\n",
      "3. : 1\n"
     ]
    }
   ],
   "source": [
    "value=input().split(\" \")\n",
    "output={}\n",
    "for item in value:\n",
    "    if item  not in list(output.keys()):\n",
    "        output[item]=1\n",
    "    else:\n",
    "        output[item]=output[item]+1        \n",
    "for key,value in output.items():\n",
    "    print(key,\":\",value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['1', '2'])\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 23\n",
    "\n",
    ">***Write a method which can calculate square value of number***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def square(n):\n",
    "    return n**2\n",
    "square(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 24\n",
    "\n",
    "\n",
    ">***Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions.***\n",
    "\n",
    ">***Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()***\n",
    "\n",
    ">***And add document for your own function***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Return the absolute value of the argument.\n",
      "\n",
      "    return the square number\n",
      "    \n"
     ]
    }
   ],
   "source": [
    "print(abs.__doc__)\n",
    "def square(n):\n",
    "    '''\n",
    "    return the square number\n",
    "    '''\n",
    "    return n**2\n",
    "print(square.__doc__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 25\n",
    "\n",
    "\n",
    ">***Define a class, which have a class parameter and have a same instance parameter.***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Person is christo\n"
     ]
    }
   ],
   "source": [
    "class person:\n",
    "    name=\"Person\"\n",
    "    def __init__(self,name):\n",
    "        self.name=name\n",
    "psn=person(\"christo\")\n",
    "print(f\"{person.name} is {psn.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 26\n",
    "\n",
    ">***Define a function which can compute the sum of two numbers.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sum(op1,op2):\n",
    "    return op1+op2\n",
    "sum(1,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 27\n",
    "\n",
    ">***Define a function that can convert a integer into a string and print it in console.***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'9'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def int2str(num):\n",
    "    return str(num)\n",
    "int2str(9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 28\n",
    "\n",
    ">***Define a function that can receive two integer numbers in string form and compute their sum and then print it in console.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sum_str(op1,op2):\n",
    "    return int(op1)+int(op2)\n",
    "sum_str(\"1\",\"2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 29\n",
    "\n",
    ">***Define a function that can accept two strings as input and concatenate them and then print it in console.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ab'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cont(str1,str2):\n",
    "    return str1+str2\n",
    "cont(\"a\",\"b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 30\n",
    "\n",
    ">***Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print all strings line by line.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc acb\n"
     ]
    }
   ],
   "source": [
    "def ret_max(str1,str2):\n",
    "    if len(str1)>len(str2):\n",
    "        return str1\n",
    "    elif len(str1)==len(str2):\n",
    "        return (str1,str2)\n",
    "    else:\n",
    "        return str2\n",
    "ret=ret_max(\"abc\",\"acb\")\n",
    "if type(ret)!=str:\n",
    "    print(ret[0],ret[1])\n",
    "else:\n",
    "    print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
